package com.colorful.insuranceproject.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.bean.BankCardBean;
import com.lovely3x.common.adapter.BaseViewHolder;
import com.lovely3x.common.adapter.ListAdapter;
import com.lovely3x.imageloader.ImageLoader;

import java.util.List;

import butterknife.Bind;

/**
 * Created by handsome-Bob on 2017/8/21.
 * <p>
 * 银行卡的适配器
 */
public class BankCardAdapter extends ListAdapter<BankCardBean> {

    public BankCardAdapter(List<BankCardBean> datas, Context context) {
        super(datas, context);
    }

    @NonNull
    @Override
    public BaseViewHolder createViewHolder(int position, ViewGroup parent) {
        return new ViewHolder(getLayoutInflater().inflate(R.layout.item_bank_card, parent, false));
    }

    @Override
    public void handleData(int position, @NonNull BaseViewHolder holder) {
        ViewHolder viewHolder = (ViewHolder) holder;
        final BankCardBean bean = datas.get(position);

        viewHolder.tvCardName.setText(bean.getBankName());

        viewHolder.tvCardNum.setText(bean.getBankCarNumber());

        if (bean.getVisiableCheckBox() == 1) {
            viewHolder.cb.setVisibility(View.VISIBLE);
        } else {
            viewHolder.cb.setVisibility(View.GONE);
        }

        if (bean.getBankCardImg().equals("")) {
            viewHolder.imgIcon.setImageResource(R.drawable.icon_coffee);
        } else {
            ImageLoader.getInstance().display(viewHolder.imgIcon, bean.getBankCardImg());
        }

        viewHolder.view.setVisibility(getCount() - 1 == position ? View.GONE : View.VISIBLE);

        viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    bean.setJudgeType(1);
                } else {
                    bean.setJudgeType(0);
                }
                notifyDataSetChanged();
            }
        });

    }

    class ViewHolder extends BaseViewHolder {

        @Bind(R.id.img_icon)
        ImageView imgIcon;

        @Bind(R.id.tv_card_name)
        TextView tvCardName;

        @Bind(R.id.tv_card_num)
        TextView tvCardNum;

        @Bind(R.id.checkbox)
        CheckBox cb;

        @Bind(R.id.view)
        View view;

        public ViewHolder(View rootView) {
            super(rootView);
        }
    }
}
